﻿@model IEnumerable<CoreLims.Models.CoreLims.CommonUnit>

@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单位类型*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="Unit-AddFactor-Select-Type" class="form-control font-rfs border-radius-noleft" style="width: 90%">
                @foreach (var item in Model)
                {
                    <option value=@item.Type>@item.Type</option>
                }
            </select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="Unit-AddFactor-Input-Type" 
                class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">单位代码*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="Unit-AddFactor-Select-Source-Code" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">正向因子*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddFactor-AscFactor" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">逆向因子*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="Unit-AddFactor-DescFactor" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div><br />
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">目标代码*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="Unit-AddFactor-Select-Target-Code" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
        </div>
    </div><br />
    <div class="text-right">
        <button id="Unit-AddFactor-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Unit-AddFactor-cancel" type="button" class="btn btn-info font-rfs" data-dismiss="modal">取消</button>
    </div>
</div>

<script>
    $("#Unit-AddFactor-Select-Type").prepend(new Option("", ""));
    $("#Unit-AddFactor-Select-Type").val("");

    $("#Unit-AddFactor-Select-Type").change(function () {
        $("#Unit-AddFactor-Input-Type").val($("#Unit-AddFactor-Select-Type").find("option:selected").text());
        $("#Unit-AddFactor-Select-Source-Code").empty();
        $("#Unit-AddFactor-Select-Target-Code").empty();

        $.post("GetUnitCodeByType", {
            Type: $("#Unit-AddFactor-Select-Type").find("option:selected").text()
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#Unit-AddFactor-Select-Source-Code").append('<option value="' + data[i].code + '">' + data[i].code + '</option>');
                $("#Unit-AddFactor-Select-Target-Code").append('<option value="' + data[i].code + '">' + data[i].code + '</option>');
            }
        });
    });

    $("#Unit-AddFactor-confirm").click(function () {
        if ($("#Unit-AddFactor-Input-Type").val().length == 0) {
            alert("单位类型不能为空！");
            $("#Unit-AddFactor-Input-Type").addClass('error-style');
            return false;
        }

        var st = $("#Unit-AddFactor-AscFactor").val();
        if (st.replace(/[\s]/g, '').length == 0 | isNaN(st)) {
            alert("转换因子不能为空,且必须是数字！");
            $("#Unit-AddFactor-AscFactor").addClass('error-style');
            return false;
        }

        var st = $("#Unit-AddFactor-DescFactor").val();
        if (st.replace(/[\s]/g, '').length == 0 | isNaN(st)) {
            alert("转换因子不能为空,且必须是数字！");
            $("#Unit-AddFactor-DescFactor").addClass('error-style');
            return false;
        }

        if ($("#Unit-AddFactor-Select-Source-Code").find("option:selected").text()
            == $("#Unit-AddFactor-Select-Target-Code").find("option:selected").text()) {
            alert("单位代码不能等于目标代码！");
            return false;
        }

        $.post("postAddConversionFactor", {
            Code: $("#Unit-AddFactor-Select-Source-Code").val(),
            AscFactor: $("#Unit-AddFactor-AscFactor").val().replace(/[\s]/g, ''),
            DescFactor: $("#Unit-AddFactor-DescFactor").val().replace(/[\s]/g, ''),
            TargetCode: $("#Unit-AddFactor-Select-Target-Code").val()
        }, function (data) {
            if (data.indexOf("成功") >= 0) {
                $("#Unit-AddFactor-cancel").click();
            } else {
                alert(data);
            }
        });
    });

</script>
